<!DOCTYPE html>
<html>
<head>
    <title>课程中心</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <!-- head 中 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">
    <link href="../css/swiper.min.css" rel="stylesheet" />
    <link href="../css/main.css" rel="stylesheet" />

</head>
<body>
    <div class="nav">
        <span onclick="history.go(-1)">
            <img src="../img/arrow-left.png" />
        </span>
        <span id="index_list_title_span">课程中心</span>
        <span id="btn-nav">导航</span>
    </div>
    <div class="weui-skin_android" id="androidActionsheet" style="display:none">
        <div class="weui-mask" style="visibility:visible; opacity:0.5"></div>
        <div class="weui-actionsheet">
            <div class="weui-actionsheet__menu">
                <div class="weui-actionsheet__cell" onclick="location.href='index.html'">课程中心</div>
                <!-- <div class="weui-actionsheet__cell" onclick="location.href='map.html'">党建大数据</div>
                <div class="weui-actionsheet__cell" onclick="location.href = 'http://39.104.67.0:8443/ehome/ehome.html'">党群E家</div> -->
                <div class="weui-actionsheet__cell" onclick="location.href='center.html'">考试中心</div>
                <!-- <div class="weui-actionsheet__cell" onclick="location.href='jianguan.html'">驻村云监管</div>
                <div class="weui-actionsheet__cell" onclick="location.href = 'http://39.104.67.0:8443/ehome/personnel.html'">人才直通车</div> -->
                <div class="weui-actionsheet__cell" onclick="location.href='user.html'">个人信息</div>
            </div>
        </div>
    </div>

   

    <div class="weui-search-bar" id="searchBar">
        <form class="weui-search-bar__form">
            <div class="weui-search-bar__box">
                <i class="weui-icon-search"></i>
                <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="搜索" required="">
                <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
            </div>
            <label class="weui-search-bar__label" id="searchText">
                <i class="weui-icon-search"></i>
                <span>搜索需要的课程</span>
            </label>
        </form>
        <a href="javascript:" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
    </div>
    <div id="articleList"></div>
    <!--<div class="weui-flex list">
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item1.png" />
                </a>   
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>            
            </div>
           
        </div>
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail.html">
                    <img src="../img/item2.png" />
                </a>
                <a class="title" href="#">水城县社区群众的守望者…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="weui-flex list">
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item1.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>

        </div>
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail.html">
                    <img src="../img/item2.png" />
                </a>
                <a class="title" href="#">水城县社区群众的守望者…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="weui-flex list">
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item2.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>

        </div>
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item1.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="weui-flex list">
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail.html">
                    <img src="../img/item2.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>

        </div>
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item1.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="weui-flex list">
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail.html">
                    <img src="../img/item2.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>

        </div>
        <div class="weui-flex__item">
            <div class="item">
                <a class="cover" href="detail2.html">
                    <img src="../img/item1.png" />
                </a>
                <a class="title" href="#">六枝特区倡导党员快乐…</a>
                <div class="comments weui-flex">
                    <div class="star weui-flex__item">
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star.png" />
                        <img src="../img/star0.png" />
                    </div>
                    <div class="users weui-flex__item">
                        <img src="../img/users.png" />
                        <span>57165</span>
                    </div>
                </div>
            </div>
        </div>

    </div>-->
   
    <!-- body 最后 -->
    <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js" type='text/javascript' charset='utf-8'></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js" type='text/javascript' charset='utf-8'></script>
    <script src="https://g.alicdn.com/dingding/open-develop/1.9.0/dingtalk.js" type='text/javascript' charset='utf-8'></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.2.6/js/swiper.min.js" type='text/javascript' charset='utf-8'></script>
    <script src="../scripts/main.js"></script>
    <!-- Initialize Swiper -->
    <script>
        var swiper = new Swiper('.swiper-container', {
            pagination: {
                el: '.swiper-pagination',
            },
        });

        var html = '<div class="weui-flex list">{{inner}}</div>';
        var innerHtml = '<div class="weui-flex__item">' +
           ' <div class="item">' +
               '<a class="cover" onClick="goToDetailPage({{id}})">' +
                    // '<img src="{{cover}}" />' +
                    '<img src="http://7xrsfo.com1.z0.glb.clouddn.com/123.jpeg" />' +
                '</a>' +
                // '<a class="title" onClick="goToDetailPage({{id}})">{{title}}</a>' +
                '<a class="title" onClick="goToDetailPage({{id}})">必修课01</a>' +
                '<div class="comments weui-flex">' +
                   ' <div class="star weui-flex__item">' +
                        '<img src="../img/star.png" />' +
                        '<img src="../img/star.png" />' +
                       ' <img src="../img/star.png" />' +
                       ' <img src="../img/star.png" />' +
                       ' <img src="../img/star0.png" />' +
                   ' </div>' +
                    '<div class="users weui-flex__item">' +
                        '<img src="../img/users.png" />' +
                       ' <span>57165</span>' +
                   ' </div>' +
               ' </div>' +
           ' </div>' +
      '  </div>';

       var userinfo = {}
       var user_id = ''
       var myStudyList = [];
        $(function () {
	        initData();
            getUserInfo(location.href, initConst);
        });

       function initConst(info) {
            userInfo = info;
            user_id = userInfo.emplId
            console.log('user_id is ' + user_id)
            getArticleList()
        }

    function initData() {
	    var typeId = GetQueryString("type_id");
	    var listTitle = Number(typeId) === 1 ? '选修课程' : Number(typeId) === 2 ? '必修课程' : '付费课程'

	    $("#index_list_title_span").html(listTitle);

	    var rel = {
		    "total": 2,
		    "rows": [
			    {
				    "us_id": 16,
				    "article_id": 3,
				    "user_id": "065506030123353335",
				    "coursetype": 3,
				    "study": 0,
				    "period": 5,
				    "add_user_id": "xuehaifeng",
				    "created": "2018-08-15 13:02:18",
				    "article": {
					    "article_id": 3,
					    "article_fid": 0,
					    "title": "付费课程01",
					    "content": "&lt;p&gt;爱抚防守打法所发生的范德萨发生的&lt;/p&gt;",
					    "abstract": "付费课程01付费课程01付费课程01",
					    "status": 1,
					    "recommend": 0,
					    "coursetype": 3,
					    "credit": 0,
					    "period": 5,
					    "author": "晓伟",
					    "source": "",
					    "cover": "http://7xrsfo.com1.z0.glb.clouddn.com/123.jpeg",
					    "urls": [],
					    "comment": 0,
					    "banner": 0,
					    "user_id": "undefined",
					    "dept_id": 1,
					    "pubtime": "2018-08-13 11:22:2",
					    "updated": "2018-08-13 11:22:36",
					    "created": "2018-08-13 11:22:36",
					    "type_ids": [
						    {
							    "at_id": 3,
							    "article_id": 3,
							    "type_id": 1,
							    "type_name": "信息上报"
						    }
					    ]
				    }
			    },
			    {
				    "us_id": 14,
				    "article_id": 2,
				    "user_id": "065506030123353335",
				    "coursetype": 2,
				    "study": 0,
				    "period": 3,
				    "add_user_id": "xuehaifeng",
				    "created": "2018-08-15 13:00:32",
				    "article": {
					    "article_id": 2,
					    "article_fid": 0,
					    "title": "必修课01",
					    "content": "&lt;p&gt;阿斯蒂芬发送到发送到发&lt;/p&gt;",
					    "abstract": "必修课01必修课01必修课01",
					    "status": 1,
					    "recommend": 0,
					    "coursetype": 2,
					    "credit": 0,
					    "period": 3,
					    "author": "晓伟",
					    "source": "",
					    "cover": "http://7xrsfo.com1.z0.glb.clouddn.com/123.jpeg",
					    "urls": [],
					    "comment": 0,
					    "banner": 0,
					    "user_id": "undefined",
					    "dept_id": 1,
					    "pubtime": "2018-08-13 11:20:24",
					    "updated": "2018-08-13 11:21:17",
					    "created": "2018-08-13 11:21:17",
					    "type_ids": [
						    {
							    "at_id": 2,
							    "article_id": 2,
							    "type_id": 1,
							    "type_name": "信息上报"
						    }
					    ]
				    }
			    }
		    ]
	    };

	    myStudyList = rel.rows;

	    rel.rows = rel.rows.filter(function(item){
		    if(Number(typeId) === 2){
			    //选修课 - 不显示不是我的课程
			    const myArticle = myStudyList.find(function(study){
				    return study.article_id ===  item.article_id
			    })

			    if(myArticle){ //这是我的课程
				    return item.coursetype === Number(typeId)
			    }
			    else {
				    return false
			    }
		    }
		    else{
			    return item.coursetype === Number(typeId)
		    }
	    })
	    console.log(rel)
	    var strHtml = [];
	    var strInner = [];

	    for (var i = 0; i < rel.rows.length; i++) {

		    var page = "";

		    if (rel.rows[i].article.urls.length > 0) {
			    page = "";
		    } else {
			    page = "2";
		    }

		    strInner.push(innerHtml.replace("{{page}}", page).replace("{{id}}", rel.rows[i].article_id).replace("{{id}}", rel.rows[i].article_id).replace("{{cover}}", rel.rows[i].cover).replace("{{title}}", rel.rows[i].title));
		    if (i % 2 == 1 || i == rel.rows.length - 1) {
			    strHtml.push(html.replace("{{inner}}", strInner.join("")));
			    strInner = [];
		    }
	    }

	    $("#articleList").html(strHtml.join(""));
    }

    function getArticleList() {
        var typeId = GetQueryString("type_id");
        var listTitle = Number(typeId) === 1 ? '选修课程' : Number(typeId) === 2 ? '必修课程' : '付费课程'
   
        $("#index_list_title_span").html(listTitle);

        $.get("http://localhost:8222/study/my?user_id=" + user_id + "&limit=1000", function (data) {
            myStudyList = data.rows;
            $.get("http://localhost:8222/article?page=1&status=1&type_id=0&limit=1000", function (rel) {

            	var rel = {
		            "total": 2,
		            "rows": [
			            {
				            "us_id": 16,
				            "article_id": 3,
				            "user_id": "065506030123353335",
				            "coursetype": 3,
				            "study": 0,
				            "period": 5,
				            "add_user_id": "xuehaifeng",
				            "created": "2018-08-15 13:02:18",
				            "article": {
					            "article_id": 3,
					            "article_fid": 0,
					            "title": "付费课程01",
					            "content": "&lt;p&gt;爱抚防守打法所发生的范德萨发生的&lt;/p&gt;",
					            "abstract": "付费课程01付费课程01付费课程01",
					            "status": 1,
					            "recommend": 0,
					            "coursetype": 3,
					            "credit": 0,
					            "period": 5,
					            "author": "晓伟",
					            "source": "",
					            "cover": "http://7xrsfo.com1.z0.glb.clouddn.com/123.jpeg",
					            "urls": [],
					            "comment": 0,
					            "banner": 0,
					            "user_id": "undefined",
					            "dept_id": 1,
					            "pubtime": "2018-08-13 11:22:2",
					            "updated": "2018-08-13 11:22:36",
					            "created": "2018-08-13 11:22:36",
					            "type_ids": [
						            {
							            "at_id": 3,
							            "article_id": 3,
							            "type_id": 1,
							            "type_name": "信息上报"
						            }
					            ]
				            }
			            },
			            {
				            "us_id": 14,
				            "article_id": 2,
				            "user_id": "065506030123353335",
				            "coursetype": 2,
				            "study": 0,
				            "period": 3,
				            "add_user_id": "xuehaifeng",
				            "created": "2018-08-15 13:00:32",
				            "article": {
					            "article_id": 2,
					            "article_fid": 0,
					            "title": "必修课01",
					            "content": "&lt;p&gt;阿斯蒂芬发送到发送到发&lt;/p&gt;",
					            "abstract": "必修课01必修课01必修课01",
					            "status": 1,
					            "recommend": 0,
					            "coursetype": 2,
					            "credit": 0,
					            "period": 3,
					            "author": "晓伟",
					            "source": "",
					            "cover": "http://7xrsfo.com1.z0.glb.clouddn.com/123.jpeg",
					            "urls": [],
					            "comment": 0,
					            "banner": 0,
					            "user_id": "undefined",
					            "dept_id": 1,
					            "pubtime": "2018-08-13 11:20:24",
					            "updated": "2018-08-13 11:21:17",
					            "created": "2018-08-13 11:21:17",
					            "type_ids": [
						            {
							            "at_id": 2,
							            "article_id": 2,
							            "type_id": 1,
							            "type_name": "信息上报"
						            }
					            ]
				            }
			            }
		            ]
	            };

            	alert(Number(typeId));

                rel.rows = rel.rows.filter(function(item){
                    if(Number(typeId) === 2){
                        //选修课 - 不显示不是我的课程
                        const myArticle = myStudyList.find(function(study){
                            return study.article_id ===  item.article_id
                        })

                        if(myArticle){ //这是我的课程
                            return item.coursetype === Number(typeId)
                        }
                        else {
                            return false
                        }
                    }
                    else{
                        return item.coursetype === Number(typeId)
                    }
                })
                console.log(rel)
                var strHtml = [];
                var strInner = [];

                for (var i = 0; i < rel.rows.length; i++) {
                   
                    var page = "";

                    if (rel.rows[i].urls.length > 0) {
                        page = "";
                    } else {
                        page = "2";
                    }
                   
                    strInner.push(innerHtml.replace("{{page}}", page).replace("{{id}}", rel.rows[i].article_id).replace("{{id}}", rel.rows[i].article_id).replace("{{cover}}", rel.rows[i].cover).replace("{{title}}", rel.rows[i].title));
                    if (i % 2 == 1 || i == rel.rows.length - 1) {
                        strHtml.push(html.replace("{{inner}}", strInner.join("")));
                        strInner = [];
                    }
                }

                $("#articleList").html(strHtml.join(""));
            });
        });
   }

    function goToDetailPage(id){
        console.log('id is ' + id)
        var typeId = Number(GetQueryString("type_id"));
        if(typeId===3){
            //付费课课 - 不是我的课程不能点击进入详情 - 弹框
            const myArticle = myStudyList.find(function(study){
                return study.article_id === Number(id)
            })

            if(myArticle){ //这是我的课程
                window.location.href = "detail2.html?id=" + id
            }
            else {
                //不是我的课程不能点击进入详情 - 弹框
                alert("改课程为付费课程, 请联系管理员付费!")
            }
        }
        else {
            window.location.href = "detail2.html?id=" + id
        }
    }
    </script>
</body>
</html>
